<%@page import="org.lzy.pt.bean.Plugin"%>
<%@page import="org.lzy.pt.PluginManager"%>
<%@page import="java.util.List"%>
<%@page import="org.lzy.pt.bean.TaskType"%>
<%@page import="org.lzy.pt.TaskManager"%>
<%@page import="org.lzy.pt.bean.Task"%>
<%@ page import="org.jivesoftware.util.*,
                 org.jivesoftware.openfire.user.*,
                 java.net.URLEncoder,
                 gnu.inet.encoding.Stringprep,
                 gnu.inet.encoding.StringprepException"
    errorPage="error.jsp"
%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="org.xmpp.packet.JID"%>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>

<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager"  />
<% webManager.init(request, response, session, application, out ); %>

<%  // Get parameters //
    boolean another = request.getParameter("another") != null;
    boolean create = another || request.getParameter("create") != null;
    boolean cancel = request.getParameter("cancel") != null;
    
    String username = ParamUtils.getParameter(request,"username");
    String T_Name = ParamUtils.getParameter(request,"T_Name");
    String T_ExecDateTime = ParamUtils.getParameter(request,"T_ExecDateTime");
    String T_Para = ParamUtils.getParameter(request,"T_Para");
    String T_Cnt = ParamUtils.getParameter(request,"T_Cnt");
    String T_Interval = ParamUtils.getParameter(request,"T_Interval");
    String T_MainVer = ParamUtils.getParameter(request,"T_MainVer");
    String T_TaskVer = ParamUtils.getParameter(request,"T_TaskVer");
    String T_FuncName = ParamUtils.getParameter(request,"T_FuncName");
    String T_FuncVer = ParamUtils.getParameter(request,"T_FuncVer");
    String T_FuncName_CN = ParamUtils.getParameter(request,"T_FuncName_CN");
    
    List<Plugin> mainPlugins = PluginManager.getInstance().getMainPlugin();
    if(!mainPlugins.isEmpty()){
    	T_MainVer = String.valueOf( mainPlugins.get(0).getT_Ver() );
    }
    
    List<Plugin> taskPlugins = PluginManager.getInstance().getTaskPlugin();
    if(!taskPlugins.isEmpty()){
    	T_TaskVer = String.valueOf( taskPlugins.get(0).getT_Ver() );
    }

    Map<String, String> errors = new HashMap<String, String>();
    // Handle a cancel
    if (cancel) {
        response.sendRedirect("task-summary.jsp");
        return;
    }

    // Handle a request to create a user:
    if (create) {
        // Validate
        if (username == null) {
            errors.put("username","");
        }
        else {
            try {
                username = username.trim().toLowerCase();
                username = JID.escapeNode(username);
                username = Stringprep.nodeprep(username);
            }
            catch (StringprepException se) {
                errors.put("username", "");
            }
        }
        
        if (T_Name == null || T_Name.equals("")) {
            errors.put("T_Name","");
        }
        
        if(T_MainVer == null || T_MainVer.equals("")){
        	errors.put("T_MainVer","");
        }
        
        if(T_TaskVer == null || T_TaskVer.equals("")){
        	errors.put("T_TaskVer","");
        }
        
        // do a create if there were no errors
        if (errors.size() == 0) {
            try {
            	
            	TaskManager.getInstance().addTask(username, T_Name, T_ExecDateTime, T_Para, T_Cnt, T_Interval, T_MainVer,T_TaskVer, T_FuncName, T_FuncVer);
            	
                // Successful, so redirect
                if (another) {
                    response.sendRedirect("task-create.jsp?success=true");
                }
                else {
                    response.sendRedirect("task-summary.jsp");
                }
                return;
            }
            catch (Exception e) {
                errors.put("general","");
                Log.error(e);
            }
        }
    }
%>

<html>
    <head>
        <title><fmt:message key="task.create.title"/></title>
        <meta name="pageID" content="task-create"/>
        <script src="JSCal2/js/jscal2.js"></script>
	    <script src="JSCal2/js/lang/cn.js"></script>
	    <link rel="stylesheet" type="text/css" href="JSCal2/css/jscal2.css" />
	    
	    <script src="dwr/engine.js" type="text/javascript"></script>
		<script src="dwr/util.js" type="text/javascript"></script>
		<script src="dwr/interface/PluginManager.js" type="text/javascript"></script>
	    
		<script type="text/javascript">
			function openModal() {
                var win = window.open(
                              'user-browser.jsp?formName=f&elName=username', 'newWin',
                              'width=800,height=550,menubar=yes,location=no,personalbar=no,scrollbars=yes,resize=yes');
			}	   
			
			function handleData(data){
				if(data==null || data.length==0){
					document.getElementById("T_FuncName").value="";
					document.getElementById("T_FuncVer").value="";
					document.getElementById("T_FuncName_CN").value="";
				}else{
					var d = data[0];
					document.getElementById("T_FuncName").value=d.t_Name;
					document.getElementById("T_FuncVer").value=d.t_Ver;
					document.getElementById("T_FuncName_CN").value=d.t_CnName;
				}
			}
			
			function onTaskTypeChange(selectedValue){
				PluginManager.getPluginByName(selectedValue,{callback:handleData});
			}
		</script>
    </head>
    <body>

<p><fmt:message key="task.create.info" /></p>

<%  if (!errors.isEmpty()) { %>

    <div class="jive-error">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr>
            <td class="jive-icon"><img src="images/error-16x16.gif" width="16" height="16" border="0" alt=""/></td>
            <td class="jive-icon-label">

            <% if (errors.get("general") != null) { %>
                <fmt:message key="task.create.error_creating_task" />
            <% } else if(errors.get("T_MainVer") != null ){ %>
                T_MainVer null
            <% } else if(errors.get("T_TaskVer") != null ){ %>
                T_TaskVer null
            <% } else { %>
                <fmt:message key="error.unknown" />
            <% } %>
            </td>
        </tr>
    </tbody>
    </table>
    </div>
    <br>

<%  } else if (request.getParameter("success") != null) { %>

    <div class="jive-success">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr><td class="jive-icon"><img src="images/success-16x16.gif" width="16" height="16" border="0" alt=""></td>
        <td class="jive-icon-label">
        <fmt:message key="task.create.created_success" />
        </td></tr>
    </tbody>
    </table>
    </div><br>

<%  } %>

<form name="f" action="task-create.jsp" method="get">

	<div class="jive-contentBoxHeader">
		<fmt:message key="task.create.new_task" />
	</div>
	<div class="jive-contentBox">
		<table cellpadding="3" cellspacing="0" border="0">
		<tbody>
		<tr>
			<td width="1%" nowrap><label for="username"><fmt:message key="task.username" />:</label> *</td>
			<td width="99%">
				<input readonly="readonly" type="text" name="username" size="30" maxlength="75" id="username">
				<button onclick="openModal()"><fmt:message key="user.select"/></button>
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_Name"><fmt:message key="task.T_Name" />:</label>*</td>
			<td width="99%">
<%-- 				<input type="text" name="T_Name" size="30" maxlength="75" value="<%= ((T_Name!=null) ? T_Name : "") %>" 
 				 id="T_Name"> --%>
				<%
					List<TaskType> taskTypes = TaskManager.getInstance().queryTaskTyprList();
				%>
				<select size="1" name="T_Name" onchange="onTaskTypeChange(this.value)">
					<option value="" ><fmt:message key="taskType.please.select"></fmt:message></option>
				    <% 
				    if (taskTypes != null && taskTypes.size() > 0){
				    	for (TaskType taskType : taskTypes) { 
				    %>
					    <option value="<%=taskType.getT_Name() %>" <% if(T_Name!=null && T_Name.equals(taskType.getT_Name())){ %> selected="selected" <%  } %> ><%=taskType.getT_CnName() %></option>
				    <% } 
				    }
				    %>
				    
				</select>
			
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_ExecDateTime_show"><fmt:message key="task.T_ExecDateTime" />:</label></td>
			<td width="99%">
				<input type="text" name="T_ExecDateTime_show" size="30" maxlength="75"
				 id="T_ExecDateTime_show" readonly="readonly">
				 <input type="hidden" id="T_ExecDateTime" name="T_ExecDateTime" />
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_Para"><fmt:message key="task.T_Para" />:</label></td>
			<td width="99%">
				<input type="text" name="T_Para" size="30" maxlength="75" value="<%= ((T_Para!=null) ? T_Para : "") %>"
				 id="T_Para">
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_Cnt"><fmt:message key="task.T_Cnt" />:</label></td>
			<td width="99%">
				<input type="text" name="T_Cnt" size="30" maxlength="75" value="<%= ((T_Cnt!=null) ? T_Cnt : "") %>"
				 id="T_Cnt">
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_Interval"><fmt:message key="task.T_Interval" />:</label></td>
			<td width="99%">
				<input type="text" name="T_Interval" size="30" maxlength="75" value="<%= ((T_Interval!=null) ? T_Interval : "") %>"
				 id="T_Interval">
			</td>
		</tr>
		
		<tr>
			<td nowrap><label for="T_MainVer"><fmt:message key="task.T_MainVer" />:</label></td>
			<td width="99%">
				<input type="text" name="T_MainVer" size="30" maxlength="75" readonly="readonly" value="<%= ((T_MainVer!=null) ? T_MainVer : "") %>"
				 id="T_MainVer">
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_TaskVer"><fmt:message key="task.T_TaskVer" />:</label></td>
			<td width="99%">
				<input type="text" name="T_TaskVer" size="30" maxlength="75" readonly="readonly" value="<%= ((T_TaskVer!=null) ? T_TaskVer : "") %>"
				 id="T_TaskVer">
			</td>
		</tr>
		
		<tr>
			<td nowrap><label for="T_FuncName"><fmt:message key="task.T_FuncName" />:</label></td>
			<td width="99%">
				<input type="text" name="T_FuncName_CN" size="30" maxlength="75" readonly="readonly" value="<%= ((T_FuncName_CN!=null) ? T_FuncName_CN : "") %>"
				 id="T_FuncName_CN">
				 <input type="hidden" name="T_FuncName" value="<%= ((T_FuncName!=null) ? T_FuncName : "") %>"
				 id="T_FuncName">
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_FuncVer"><fmt:message key="task.T_FuncVer" />:</label></td>
			<td width="99%">
				<input type="text" name="T_FuncVer" size="30" maxlength="75" readonly="readonly" value="<%= ((T_FuncVer!=null) ? T_FuncVer : "") %>"
				 id="T_FuncVer">
			</td>
		</tr>
		
        <tr>
			<td colspan="2" style="padding-top: 10px;">
				<input type="submit" name="create" value="<fmt:message key="task.create.create" />">
				<input type="submit" name="another" value="<fmt:message key="task.create.create_another" />">
				<input type="submit" name="cancel" value="<fmt:message key="global.cancel" />"></td>
		</tr>
		</tbody>
		</table>

	</div>

	<span class="jive-description">
    * <fmt:message key="user.create.requied" />
    </span>
</form>

<script language="JavaScript" type="text/javascript">
document.f.username.focus();
Calendar.setup({
    inputField : "T_ExecDateTime_show",
    trigger    : "T_ExecDateTime_show",
    onSelect   : function() { 
    	this.hide();
    	document.getElementById("T_ExecDateTime").value = this.selection.get().toString()+this.getTime();
    	//document.getElementByName("T_ExecDateTime").value=Calendar;
    },
    showTime   : 12,
    dateFormat : "%Y-%m-%d %I:%M %p"
  });
</script>
    </body>
</html>